function [h] = plot_ts(s1,gaptime,options)
%PLOT_TS	plot time series with gaps identification
%	[h] = plot_ts(s1,gaptime,options) 
%	
%
%	Inputs:     s1          time series (Nx2), or cell array with multiple time series 
%                           (one per cell)
%               gaptime     define gap time (time interval to be considered a
%                           gap
%               options     structure with plot options
%		
%		
%
%	Outputs:    h           figure handle
%		
%		
%	See also explore_data.m 

%	References: N/A
%	
%	

%	Copyright 2013 MAF Pimentel
%	This program is free software: you can redistribute it and/or modify
%	it under the terms of the GNU General Public License as published by
%	the Free Software Foundation, either version 3 of the License, or
%	(at your option) any later version.
%	
%	This program is distributed in the hope that it will be useful,
%	but WITHOUT ANY WARRANTY; without even the implied warranty of
%	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%	GNU General Public License for more details.
%	
%	You should have received a copy of the GNU General Public License
%	along with this program.  If not, see <http://www.gnu.org/licenses/>.


%	$Author: MAF Pimentel$
%	$Revision: 1.0.0.0$
%	$Date: 30-May-2013 11:35:34$
%	Contact: marco.and.pimentel@gmail.com
%	Originally written on: PCWIN64

if nargin < 2 
    gaptime = 120;
end

h = figure;
if iscell(s1)
    if length(s1) >= 4
        fprintf('\nCode optimized for up to 3 time series!\n');
        return;
    else
        xmaxval = zeros(length(s1),1); xminval = zeros(length(s1),1);
        f = zeros(length(s1),1);
        x = [];
        for i = 1 : length(s1)
            f(i) = subplot(length(s1),1,i);
            plot(s1{i}(:,1),s1{i}(:,2),'-or','MarkerEdgeColor','k','MarkerFaceColor','r');
            ylim([min(s1{i}(:,2))-5 max(s1{i}(:,2))+5]);
            dt = diff(s1{i}(:,1));
            gaps = (max(s1{i}(:,2))+5)*(dt > gaptime); gaps = gaps(:);
            x = [s1{i}(1:end-1,1) + 5; s1{i}(2:end,1) - 5]; x = sort(x);
            gapsf = zeros(2*length(gaps),1);
            gapsf(1:2:end) = gaps; gapsf(2:2:end) = gaps;
            area(x,gapsf,'FaceColor',[.6 .6 .6]);
            alpha(0.8);
            xminval(i) = min(s1{i}(:,1)); xmaxval(i) = max(s1{i}(:,1));
        end
        xlim([min(xminval) max(xmaxval)]);
    end
else
    plot(s1(:,1),s1(:,2),'-or','MarkerEdgeColor','k','MarkerFaceColor','r');
    ylim([min(s1(:,2))-5 max(s1(:,2))+5]);
    hold on
    dt = diff(s1(:,1));
    gaps = (max(s1(:,2))+5)*(dt > gaptime); gaps = [gaps(:)];
    x = [s1(1:end-1,1) + 5; s1(2:end,1) - 5]; x = sort(x);
    gapsf = zeros(2*length(gaps),1);
    gapsf(1:2:end) = gaps; gapsf(2:2:end) = gaps;
    area(x,gapsf,'FaceColor',[.6 .6 .6]);
    alpha(0.8);
end